package leetcode.offer_two_special.tree;

import java.util.TreeSet;

/**
 * @author mazouri
 * @create 2021-09-15 22:08
 */
public class _57_containsNearbyAlmostDuplicate {
    public boolean containsNearbyAlmostDuplicate1(int[] nums, int k, int t) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < i + k + 1 && j < nums.length; j++) {
                System.out.println("i: " + i + "  j:  " + j);
                System.out.println("abs:   " + Math.abs(nums[i] - nums[j]));
                if (Math.abs((long) nums[i] - (long) nums[j]) <= t) return true;
            }
        }
        return false;
    }

    public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        TreeSet<Long> set = new TreeSet<>();

        for (int i = 0; i < nums.length; i++) {

            Long lowNum = set.floor((long) nums[i]);
            if (lowNum != null && lowNum >= (long)nums[i] - t) return true;

            Long bigSum = set.ceiling((long) nums[i]);
            if (bigSum != null && bigSum <= (long)nums[i] + t) return true;

            set.add((long) nums[i]);

            if (i >= k) set.remove((long) nums[i - k]);
        }

        return false;
    }

    //[-2147483648,2147483647]
    //1
    //1
    public static void main(String[] args) {
        TreeSet<Long> set = new TreeSet<>();
        Long floor = set.floor(5L);
        System.out.println(floor);
        System.out.println(floor > 3);
    }
}
